load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

genrule(
    name = "flatten_header_vm",
    srcs = [
        "gen_header.sh",
        "docs.bzl",
    ],
    outs = ["flatten_header.vm"],
    cmd = "$(location gen_header.sh) $(location docs.bzl) $@",
    output_to_bindir = True,
)

# Note that the stardoc `out` files are prefixed with `stardoc_` so the
# `diff_test` targets which ensure documentation is up to date can access the
# committed markdown files instead of the `out` targets from the stardoc rules

stardoc(
    name = "flatten_docs",
    out = "stardoc_flatten.md",
    header_template = ":flatten_header_vm",
    input = "docs.bzl",
    tags = ["no-sandbox"],  # https://github.com/bazelbuild/stardoc/issues/112
    deps = [
        "@rules_foreign_cc//foreign_cc:defs",
        "@rules_foreign_cc//foreign_cc:repositories",
    ] + [
        "@rules_foreign_cc//foreign_cc:cmake",
        "@rules_foreign_cc//foreign_cc:configure",
        "@rules_foreign_cc//foreign_cc:make",
        "@rules_foreign_cc//foreign_cc:meson",
        "@rules_foreign_cc//foreign_cc:ninja",
        "@rules_foreign_cc//foreign_cc:providers",
    ],
)

stardoc(
    name = "cmake_docs",
    out = "stardoc_cmake.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:cmake.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:cmake"],
)

stardoc(
    name = "make_docs",
    out = "stardoc_make.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:make.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:make"],
)

stardoc(
    name = "ninja_docs",
    out = "stardoc_ninja.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:ninja.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:ninja"],
)

stardoc(
    name = "configure_make_docs",
    out = "stardoc_configure_make.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:configure.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:configure"],
)

stardoc(
    name = "meson_docs",
    out = "stardoc_meson.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:meson.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:meson"],
)

stardoc(
    name = "providers_docs",
    out = "stardoc_providers.md",
    header_template = "common_header.vm",
    input = "@rules_foreign_cc//foreign_cc:providers.bzl",
    deps = ["@rules_foreign_cc//foreign_cc:providers"],
)

DOCS_TARGETS = [
    ":flatten_docs",
    ":cmake_docs",
    ":ninja_docs",
    ":make_docs",
    ":configure_make_docs",
    ":meson_docs",
    ":providers_docs",
]

build_test(
    name = "docs_build_test",
    targets = DOCS_TARGETS,
    visibility = ["//visibility:public"],
)

# Run this target to upate/generate docs
genrule(
    name = "generate_docs_src",
    srcs = DOCS_TARGETS + [
        "index.md",
        "rules.md",
    ],
    outs = ["generate_docs.sh"],
    cmd = """\
export COMMIT="$$(cat bazel-out/stable-status.txt | grep STABLE_SCM_VERSION | cut -d' ' -f2)"
export SHORT_COMMIT="$$(cat bazel-out/stable-status.txt | grep STABLE_SCM_SHORT_VERSION | cut -d' ' -f2)"
export RELEASE="$$(cat bazel-out/stable-status.txt | grep STABLE_RELEASE | cut -d' ' -f2)"
cat << EOF > $@
#!/usr/bin/env bash
set -euo pipefail

short_commit="$${SHORT_COMMIT}"
commit="$${COMMIT}"
release="$${RELEASE}"
release_archive="$${RELEASE}"

# TODO: If a release is set, we assume it's set to a branch name.
# thus we default the archive value to a commit. This is likely
# only appropriate when publishing on the branch in specified
# and a smarter solution should be found to avoid unexpected behavior. 
if [[ -n "\\$${RELEASE:-}" ]]; then
    release="\\$${RELEASE}"
    release_archive="\\$${commit}"
fi

mkdir -p \\$${BUILD_WORKSPACE_DIRECTORY}/src
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:flatten_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/flatten.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:cmake_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/cmake.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:ninja_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/ninja.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:make_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/make.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:meson_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/meson.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:configure_make_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/configure_make.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(location //:providers_docs) > \\$${BUILD_WORKSPACE_DIRECTORY}/src/providers.md
# Non generated sources
cp \\$${BUILD_WORKSPACE_DIRECTORY}/$(rootpath rules.md) \\$${BUILD_WORKSPACE_DIRECTORY}/src/rules.md
cat \\$${BUILD_WORKSPACE_DIRECTORY}/$(rootpath index.md)  \
| sed "s/{short_commit}/\\$${short_commit}/" \
| sed "s/{commit}/\\$${commit}/" \
| sed "s/{release}/\\$${release}/" \
| sed "s/{release_archive}/\\$${release_archive}/" \
> \\$${BUILD_WORKSPACE_DIRECTORY}/src/index.md
EOF
""",
    stamp = True,
)

sh_binary(
    name = "generate_docs",
    srcs = [":generate_docs_src"],
    data = DOCS_TARGETS,
)
